package cn.hyman.producer;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

@Component
public class QueueProducer {

    private ActiveMQQueue activeMQQueue;
    private JmsMessagingTemplate jmsMessagingTemplate;

    //构造注入对象(推荐)

    @Autowired
    public QueueProducer(JmsMessagingTemplate jmsMessagingTemplate, ActiveMQQueue activeMQQueue) {
        this.jmsMessagingTemplate = jmsMessagingTemplate;
        this.activeMQQueue = activeMQQueue;
    }

    //发送Queue的方法
    public void producerMsg() {
        jmsMessagingTemplate.convertAndSend(activeMQQueue, "**************" + UUID.randomUUID().toString());
    }

    //发送Queue的方法
    @Scheduled(fixedDelay = 3000L)
    public void producerMsgScheduled() {
        jmsMessagingTemplate.convertAndSend(activeMQQueue, "*******producerMsgScheduled*******" + UUID.randomUUID().toString());
        System.out.println("producerMsgScheduled send---");
    }


}
